﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace GaussianGen
{
     public partial class Form1 : Form
     {
          Random rand = new Random();
          public int sum = 1;
          public bool run = false;
          int[] r;
          int num = 0;
          int max = 500;
          double mean = 0,
                 var = 1;

          public Form1()
          {
               InitializeComponent();
               this.DoubleBuffered = true;
               SetStyle(ControlStyles.AllPaintingInWmPaint, true);
               SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
               UpdateStyles();
               r = new int[max];
               for (int i = 0; i < max; i++)
                    r[i] = 0;
               
          }

          private void Form1_Paint(object sender, PaintEventArgs e)
          {
               Graphics g = e.Graphics;
               Pen p = new Pen(Color.Red);
               Font f = new Font("Times New Roman", 10);
               if (sum != 0)
               {
                     for (int i = 0; i < r.Length; i++)
                    {
                         g.DrawLine(p, 10 + i, 300, 10 + i, 300 - (int)(150 * 100 * ((double)r[i] / (double)sum)));
                         if (i % (max / 10) == 0)
                              g.DrawString("" + ((i * (var / max))  + (mean - (var / 2))), f, new SolidBrush(Color.Black), 5 + i, 301);
                    }
                     g.DrawString("" + (mean + (var / 2)), f, new SolidBrush(Color.Black), 5 + max, 301);
                     g.DrawString("Abisyn, 09.2011r.", f, new SolidBrush(Color.Brown), 10, 370);
               }
          }

          private void button1_Click(object sender, EventArgs e)
          {
               run = !run;
          }

          private void timer1_Tick(object sender, EventArgs e)
          {
               if (run)
               {
                    mean = (double)numericUpDown1.Value;
                    var = (double)numericUpDown2.Value;
                    num += 100;
                    label1.Text = "ilość losowań: " + num;
                    for (int i = 0; i < 100; i++)
                    {
                         double u1 = rand.NextDouble();
                         double u2 = rand.NextDouble();
                         double randStdNormal = mean + Math.Sqrt(var) * Math.Sqrt(-2 * Math.Log(u1)) * Math.Cos(2 * Math.PI * u2); //random normal(0,1)
                         if (randStdNormal > (mean - (var / 2)) && randStdNormal < (mean + (var / 2)))
                         {
                              double frct = var / max;
                              int idx = (int)((randStdNormal - (mean - (var / 2))) / frct);
                              r[idx] += 1;
                         }

                    }
                    sum = 0;
                    for (int i = 0; i < max; i++)
                         sum += r[i];
                    Refresh();
                    
               }
          }

          private void button2_Click(object sender, EventArgs e)
          {
               for (int i = 0; i < r.Length; i++)
                    r[i] = 0;
               sum = 0;
               num = 0;
               Refresh();
          }


     }
}
